package com.ztic;

import com.sun.jna.ptr.IntByReference;

import java.awt.event.KeyAdapter;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;


//线程同步数据类
class ThdSynchObj
{
    public Em9118      objDev;              //
    public Object      objSyn;             //线程同步对象
    public boolean     bProc;              //子线程是否继续读取数据
}

//读取fifo数据工作线程
class thdWork implements  Runnable
{
    public boolean bRun=true;
    int i = 0;
    public ThdSynchObj objSyn;
    private double[][] df2Val;

    private  void disp()
    {
        //显示均值+抖动
        int iChInx;//通道号索引，实际通道号-1
        int iCodeInx;//每个通道的数据索引
        int i=0;//采集数据二维数组的行，注意只有在1通道使能且后续通道号连续使能的时候，i和iChInx才是相等的。

        double[] dfAvr = new double[Em9118.EM9118_MAXADCHCNT];
        double[] dfDiff = new double[Em9118.EM9118_MAXADCHCNT];
        //计算
        for(iChInx=0;iChInx<Em9118.EM9118_MAXADCHCNT;iChInx++) {
            if (objSyn.objDev.iIsAdInFifo[iChInx] == 0)
                continue;
            double dfMax = Double.MIN_VALUE;
            double dfMin = Double.MAX_VALUE;
            dfAvr[iChInx] = 0;
            for (iCodeInx = 0; iCodeInx < df2Val[i].length; iCodeInx++) {
                if (df2Val[i][iCodeInx] > dfMax)
                    dfMax = df2Val[i][iCodeInx];
                if (df2Val[i][iCodeInx] < dfMin)
                    dfMin = df2Val[i][iCodeInx];
                dfAvr[i] += df2Val[i][iCodeInx];
            }
            dfAvr[iChInx] /= df2Val[i].length;
            dfDiff[iChInx] = dfMax - dfMin;
            ++i;
        }
        //显示
        //清屏幕
        try{
            new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
        }
        catch(Exception e)
        {
            System.out.println(e.toString());
        }
        System.out.println("***********************中泰联创EM9118模数采集模块JAVA演示程序******************************!");
        System.out.println("通道      平均值      差值");
        for(iChInx=0;iChInx<Em9118.EM9118_MAXADCHCNT;iChInx++) {
            if (objSyn.objDev.iIsAdInFifo[iChInx] == 0)
                continue;
            if(!objSyn.objDev.bAdType) {
                //显示物理值
                System.out.println("ch" + (iChInx + 1) + String.format("%10.1f", dfAvr[iChInx] * 1000) + "mV" + String.format("%10.1f", dfDiff[iChInx] * 1000) + "mV");
            } else {
                //显示原码
                System.out.println("ch" +(iChInx + 1) + String.format("%10.0f",dfAvr[iChInx]) + String.format("%10.0f",dfDiff[iChInx]) + "");
            }
        }
        //显示提示信息
        System.out.println("Pls press ENTER terminate program!");
    }

    @Override
    public void run()
    {
        do
        {
           //进入同步块
            synchronized(objSyn.objSyn)
            {
                if(objSyn.bProc)
                {
                    df2Val=objSyn.objDev.ReadAdVal();
                }else
                    break;
                //将数据按照平均值和抖动值显示出来
                disp();
            }
        }while(true);
    }
}

public class Main {

    public static void main(String[] args)
            throws Exception {
	// write your code here
        Em9118 objDev=new Em9118("192.168.1.149");
        int iE=objDev.DevConn();
        //设置AD参数
        //设置AD1~AD6通道进Fifo,采集范围±10V，采集频率10000K，使用内时钟定时启动采集
        int i1;
        for(i1=0;i1<Em9118.EM9118_MAXADCHCNT;i1++)
        {
            if(i1<6)
                objDev.iIsAdInFifo[i1]=1;
            else
                objDev.iIsAdInFifo[i1]=0;
        }
        objDev.iAdRangeIdx = objDev.EM9118_AD_RANGE_N10_10V;
        objDev.dfFreq = 100000;
        objDev.iClkSrc = objDev.EM9118_CLKSRC_IN;
        objDev.iTriSrc = objDev.EM9118_TRISRC_IN;
        objDev.bAdType=false;//显示电压值
        objDev.SetReadTime( 1000 );//1秒读取一次数据并且显示


        //启动daq
        iE=objDev.StartHc();

        //声明及初始化工作线程及线程同步变量
        thdWork thdRead=new thdWork();
        thdRead.objSyn=new ThdSynchObj();
        thdRead.objSyn.objDev=objDev;
        thdRead.objSyn.objSyn=new Object();
        thdRead.objSyn.bProc=true;

        //定义并初始化键盘输入变量
        Scanner scan = new Scanner(System.in);
        String str1;
        //定义并初始化程序流程控制变量
        boolean bProc=true;
        //定义并初始化设备采集数据存储变量
        short[][]  df2AdCode=new short[Em9118.EM9118_MAXADCHCNT][objDev.iGroupCnt];     //ad采集原码值
        double[][]  df2AdVal=new double[Em9118.EM9118_MAXADCHCNT][objDev.iGroupCnt];      //ad采集物理量值

        Thread thdWork=new Thread(thdRead);
        thdWork.start();

        //Thread.sleep(300);
        //Thread.yield();

        //清屏幕
        try{
            new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
        }
        catch(Exception e)
        {
            System.out.println(e.toString());
        }
        //等待并判断用户按键输入
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String str = bf.readLine();
        //通知工作线程退出
        synchronized(thdRead.objSyn.objDev)
        {
            thdRead.objSyn.bProc=false;
        }
        //停止数据读取线程及daq
        objDev.StopHc();

        //断开与设备的连接
        objDev.DevDisConn();

        //尝试让Java的GC执行finalize()方法。
        objDev=null;
        Thread.sleep(500);
        System.gc();

    }

    //endregion
}
